#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;


int longestArithSeqLength(vector<int>& nums) {
    unordered_map<int, int>hash;
    int n = nums.size();
    hash[nums[0]] = 0;

    vector<vector<int>>dp(n, vector<int>(n, 2));
    int ret = 2;

    for (int i = 1; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            int tar = 2 * nums[i] - nums[j];
            if (hash.count(tar))
                dp[i][j] = dp[hash[tar]][i] + 1;
            ret = max(ret, dp[i][j]);
        }
        hash[nums[i]] = i;
    }

    return ret;
}

int main()
{
    vector<int>nums = { 20, 1, 15, 3, 10, 5, 8 };
    cout << longestArithSeqLength(nums) << endl;
    return 0;
}